<?php

class Default_DebatesController extends SON_Controller_Action
{

    public function indexAction()
    {
    	$candidato = new Candidato();
    	$rcandidato = $candidato->find((int) $this->_getParam('candidato'));
    	
    	if ($rcandidato) {
    	
	    	$form = new SON_Forms_Busca();
	        $debate = new Debate();
	        
	        $conditions = array('debate.candidato_id = ?' => (int) $this->_getParam('candidato'));
	        
	        if ($this->_request->isPost()) {
	        	$form->populate($this->_request->getPost());
	        	$conditions += array(
	        		'debate.descricao LIKE ?' => '%' . $this->_getParam('filtro_busca') . '%'
	        	);
	        }
	        
	        $this->view->candidato = $rcandidato;
	        $this->view->form = $form;
	        $this->view->debates = $debate->search(
	            array(
	                'page' => $this->_getParam('page', 1),
	                'conditions' => $conditions
	            )
	        );
        
        } else {
        	$this->_redirect('/');
        }
    }

    public function addAction()
    {
    	$candidato = new Candidato();
    	$rcandidato = $candidato->find((int) $this->_getParam('candidato'));
    	 
    	if ($rcandidato) {
    		
	    	$form = new SON_Forms_Debate();
	    
	    	if ($this->_request->isPost()) {
	    		$data = $this->_request->getPost();
	    		if ($form->isValid($data)) {
	    			
	    			$data['candidato_id'] = $rcandidato->getId();
	    			$data['usuario_id'] = $this->userId;
	    
	    			$debate = new Debate($data);
	    			$debate->save();
	    
	    			$this->_helper->FlashMessenger(
	    				array('success' => 'Debate cadastrado com sucesso')
	    			);
	    
	    			$this->_redirect('debates/index/candidato/' . $rcandidato->getId());
	    		} else {
	    			$form->populate($data);
	    			$this->view->error = $form->getMessages();
	    		}
	    	}
	    
	    	$this->view->candidato = $rcandidato;
	    	$this->view->form = $form;
    	} else {
    		$this->_redirect('/');
    	}
    }
    
    public function discussAction()
    {
    	$debate = new Debate();
    	$rdebate = $debate->find((int) $this->_getParam('debate'));
    	
    	if ($rdebate) {
    	
    		$form = new SON_Forms_Comentario();
    		$comentario = new Comentario();
    		
    		$conditions = array('comentario.debate_id = ?' => (int) $this->_getParam('debate'));
    		 
    		if ($this->_request->isPost()) {
    			$data = $this->_request->getPost();
    			if ($form->isValid($data)) {
    	
    				$data['debate_id'] = $rdebate->getId();
    				$data['usuario_id'] = $this->userId;
    				 
    				$comentario = new Comentario($data);
    				$comentario->save();
    			} else {
    				$form->populate($data);
    				$this->view->error = $form->getMessages();
    			}
    		}
    		 
    		$this->view->debate = $rdebate;
    		$this->view->form = $form;
    		$this->view->comentarios = $comentario->search(
	            array(
	                'page' => $this->_getParam('page', 1),
	                'conditions' => $conditions
	            )
	        );
    	} else {
    		$this->_redirect('/');
    	}
    }

}

